package com.europe.business.europebusiness.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.europe.business.europebusiness.ui.adapter.viewholder.ListViewHolder;

import java.util.List;

public abstract class ListAdapter1<T> extends RecyclerView.Adapter<ListViewHolder> {

    protected List<T> mDatas;
    protected Context mContext;
    protected int mLayoutId;
    protected LayoutInflater mLayoutInflater;
    ItemClicked mItemClicked;

    public ListAdapter1(Context context, List<T> datas, int layoutId, ItemClicked itemClicked){
        mDatas = datas;
        mLayoutId = layoutId;
        mContext = context;
        mItemClicked = itemClicked;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ListViewHolder viewHolder = ListViewHolder.getViewHold(mContext, mLayoutId, viewGroup);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ListViewHolder listViewHolder, final int i) {
        T t = null;
        if(i < mDatas.size()){//最后一个加载更多item，没有data
            t = mDatas.get(i);
        }
        convert(listViewHolder, t);

        listViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClicked.onItemClicked(i,listViewHolder);
            }
        });

    }

    public abstract void convert(ListViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public interface ItemClicked{
        public void onItemClicked(int index, ListViewHolder listViewHolder);
    }

}
